package com.csj.mvvmtest;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * 多个同步网络请求，如何同时返回结果
 */
public class ThreadActivity extends AppCompatActivity {
    public static final String TAG = "ThreadActivity";
    ExecutorService mExecutor = Executors.newFixedThreadPool(5);

    public static Intent newIntent(Context context) {
        return new Intent(context, ThreadActivity.class);
    }

    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_third);

        final List<Future<String>> mFutures = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            mFutures.add(mExecutor.submit(new MyCallable(i)));
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    boolean success = true;
                    for (Future<String> future : mFutures) {
                        try {
                            final String s = future.get();
                            if (s == null) {
                                success = false;
                            } else {
                                Log.d(TAG, "run: s = " + s);
                            }
                        } catch (ExecutionException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if (success) {
                        Log.d(TAG, "run: 5个线程执行成功");
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(ThreadActivity.this, "执行成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                        break;
                    }
                }
            }
        }).start();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }

    private static class MyCallable implements Callable<String> {
        final int mInt;


        MyCallable(int i) {
            mInt = i;
        }

        @Override
        public String call() throws Exception {
            SystemClock.sleep(TimeUnit.SECONDS.toMillis(mInt));
            return String.valueOf(mInt + 1000);
        }
    }
}
